// pages/index/paypass.js
var app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type:0,
    time:60,
    getCode:'获取验证码',
    disabled:'false',
    color:'#7D7D7D',
    type1:0,
    pass1:'password',
    passimg1:'../../../img/paypass_false.png',
    type2:0,
    pass2:'password',
    passimg2:'../../../img/paypass_false.png',password1:'',password2:'',yanzhengma1:'',phone:'',last:''
  },
  onLoad: function (options) {
    this.setData({phone:options.phone})
    console.log(this.data.phone,options.phone);
    

  },
  // 获取验证码
  change(e){
   
    var value = e.detail.value;
    if(value != ''){
      this.setData({
        type:1,
        yanzhengma1:e.detail.value
      })
    }else{
      this.setData({
        type:0,
      })
    }
  },

    //倒计时
    time(e){
      
      
      var disabled = e.currentTarget.dataset.disabled
      if(disabled == true){
        return false;
      }
      this.throttle(()=>{
        this.getrecoginize()
      },1500)
   


     
  
    },

  //密码显示隐藏
  pass(e){
    var index = e.currentTarget.dataset.index
    var that = this;
    if(index == 0){
      that.setData({
        type1:1,
        pass1:'digit',
        passimg1:'../../../img/paypass_true.png',
      })
    }else{
      that.setData({
        type1:0,
        pass1:'password',
        passimg1:'../../../img/paypass_false.png',
      })
    }
  },
  pass1(e){
    var index = e.currentTarget.dataset.index
    var that = this;
    if(index == 0){
      that.setData({
        type2:1,
        pass2:'number',
        passimg2:'../../../img/paypass_true.png',
      })
    }else{
      that.setData({
        type2:0,
        pass2:'password',
        passimg2:'../../../img/paypass_false.png',
      })
    }
  },
  sureedit(){
    console.log(wx.getStorageSync('uid'));
    
    if(this.data.yanzhengma1==''||this.data.password1==''||this.data.password2==''){
      return wx.showToast({
        title: '请填写完整',
      })}
     
    let yanzhengma1=wx.getStorageSync('yanzhengma1')
      if(this.data.yanzhengma1!=''&&yanzhengma1==this.data.yanzhengma1 &&this.data.password1==this.data.password2){
        wx.request({
          url: 'https://zujieba.com/Api/member/paypass.html',
          data:{id:wx.getStorageSync('uid'),phone:this.data.phone,paypass:this.data.password1},
          method:'POST',
          success:(res)=>{
            console.log(1111111,this.data.phone);
            
            console.log(res);
            if(res.data.success==1){
             
              wx.navigateBack({
                complete: (res) => {},
              })
              wx.showToast({
                title: res.data.tips,
                icon:'success',
                duration:2000
              })
            }else{wx.showToast({
              title: res.data.tips,
              icon:'none',
              duration:2000
            })}
            
          }
        })
        
      }else{
        
        
        wx.showToast({
          title: '输入不正确',
          icon:'none'
        })
      }
     
    
  },
  //需要有手机号
  getrecoginize(){
    console.log(this.data.phone);
    
    wx.request({
      url: 'https://zujieba.com/Api/phone/code.html',
      data:{phone:this.data.phone,id:wx.getStorageSync('uid')},
      method:'POST',
      success:(res)=>{
        if(res.data.success==1){
       wx.setStorageSync('yanzhengma1', res.data.data)}else{
          wx.showToast({
            title: res.data.tips,
            duration:2000
          })
       }
        
      }
    })
    var that = this;
    var times = app.globalData.time
    console.log(times);
    
    var i = setInterval(function() {
         times--
        
         if (times <= 0) {
              that.setData({
                   color: "#7D7D7D",
                   disabled: false,
                   getCode: "获取验证码",
              })
             
             wx.removeStorageSync('time')
              clearInterval(i)
         } else {
      
          wx.setStorageSync('time', times)
              that.setData({
                   getCode: "重新获取" + times + "s",
                   color: "#09C483",
                   disabled: true
              })
             
         }
    }, 1000)
   
     
},
change1(e){
  if(e.detail.value==''){
    wx.showToast({
      title: '不能为空',
    })
  }
  this.setData({
    password1:e.detail.value
  })

},
change2(e){
  if(e.detail.value==''){
    wx.showToast({
      title: '不能为空',
    })
  }
  this.setData({
    password2:e.detail.value
  })
},
 //节流
 throttle:function(func,wait){
  console.log(func,wait);
  
  let last =this.data.last
  console.log(last);
  
  let current_time= +new Date()
  if(current_time-last>wait){
    console.log(1111);
      console.log(current_time,current_time-last);
      
      func()
      last=+new Date()
      this.setData({
        last
      })
  }
    
     
  
   
},
 
})